package com.ddtech.business.practice.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.practice.dto.RankDto;
import com.ddtech.business.practice.entity.PracticeUserRecord;
import com.ddtech.business.practice.mapper.PracticeUserRecordMapper;
import com.ddtech.business.practice.query.PracticeUserRecordQuery;
import com.ddtech.business.practice.service.PracticeUserRecordService;
import com.ddtech.config.DataScope;
import com.ddtech.framework.system.service.DeptService;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.SecurityUtils;
import com.ddtech.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 学员练习记录表 服务实现类
 * </p>
 *
 * @author bin_pu
 * @since 2023-05-26
 */
@Service
public class PracticeUserRecordServiceImpl extends ServiceImpl<PracticeUserRecordMapper, PracticeUserRecord> implements PracticeUserRecordService {
    @Resource
    private PracticeUserRecordMapper practiceUserRecordMapper;

    @Autowired
    private DataScope dataScope;

    @Autowired
    private DeptService deptService;

    @Override
    public Map<String, Object> getStatistics(String userId) {
        return practiceUserRecordMapper.getStatistics(userId);
    }

    /***
     * description:APP 今日练习数据统计
     * @param:  * @param
     * @date: 2023-07-04
     * @return: java.lang.Object
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public Object getStatisticsApp() {
        String userId= SecurityUtils.getUserId();
        SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd");
        String date=sf.format(new Date());
        return  practiceUserRecordMapper.getStatisticsApp(userId,date);
    }

    /***
     * description:APP 最近7练习数据
     * @param:  * @param
     * @date: 2023-07-04
     * @return: java.lang.Object
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public Object getSevenPracticeList() {
        String userId=SecurityUtils.getUserId();
        return  practiceUserRecordMapper.getSevenPracticeList(userId);
    }

    /***
     * description:APP 最近15天练习数据
     * @param:  * @param
     * @date: 2023-07-04
     * @return: java.lang.Object
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public Object getHalfMonthPracticeList() {
        String userId=SecurityUtils.getUserId();
        return  practiceUserRecordMapper.getHalfMonthPracticeList(userId);
    }

    @Override
    public Object pageBankList(PracticeUserRecordQuery practiceUserRecordQuery) {
        if(StringUtils.isNotEmpty(practiceUserRecordQuery.getDeptId())){
            Set<String> deptIds=this.queryDeptIds(practiceUserRecordQuery.getDeptId());
            if(deptIds.size()>0){
                practiceUserRecordQuery.setDeptIds(deptIds);
            }
        }
        if(StringUtils.isNotEmpty(practiceUserRecordQuery.getStartTime())){
            practiceUserRecordQuery.setStartTime(practiceUserRecordQuery.getStartTime()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(practiceUserRecordQuery.getEndTime())){
            practiceUserRecordQuery.setEndTime(practiceUserRecordQuery.getEndTime()+" 23:59:59");
        }
        IPage<RankDto> page = practiceUserRecordMapper.pageBankList(practiceUserRecordQuery.populatePage(), practiceUserRecordQuery);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object typeCHeckList(PracticeUserRecordQuery practiceUserRecordQuery) {
        IPage<Map<String, Object>> page = practiceUserRecordMapper.typeCHeckList(practiceUserRecordQuery.populatePage(), practiceUserRecordQuery);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object allCHeckList(PracticeUserRecordQuery practiceUserRecordQuery) {
        IPage<Map<String, Object>> page = practiceUserRecordMapper.allCHeckList(practiceUserRecordQuery.populatePage(), practiceUserRecordQuery);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object allUserGrade(PracticeUserRecordQuery practiceUserRecordQuery) {
        if(StringUtils.isNotEmpty(practiceUserRecordQuery.getDeptId())){
            Set<String> deptIds=this.queryDeptIds(practiceUserRecordQuery.getDeptId());
            if(deptIds.size()>0){
                practiceUserRecordQuery.setDeptIds(deptIds);
            }
        }
        if(StringUtils.isNotEmpty(practiceUserRecordQuery.getStartTime())){
            practiceUserRecordQuery.setStartTime(practiceUserRecordQuery.getStartTime()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(practiceUserRecordQuery.getEndTime())){
            practiceUserRecordQuery.setEndTime(practiceUserRecordQuery.getEndTime()+" 23:59:59");
        }
        IPage<Map<String, Object>> page = practiceUserRecordMapper.allUserGrade(practiceUserRecordQuery.populatePage(), practiceUserRecordQuery);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Set<String> queryDeptIds(String deptId) {
        Set<String> deptSet = new HashSet<>();
        deptSet.add(deptId);
        deptSet.addAll(dataScope.getDeptChildren(deptService.findByPid(deptId)));
        return deptSet;
    }
}
